package com.longxi.tianjintianbao.ui.fragment.mian;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.greedao.controller.DataWorkController;
import com.longxi.tianjintianbao.ui.activity.main.MainActivity;
import com.longxi.tianjintianbao.ui.base.LazyFragment;
import com.longxi.tianjintianbao.ui.presenter.main.HomeFgPresenter;
import com.longxi.tianjintianbao.ui.view.main.IHomeFgView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

/**
 * @创建者 Kuma
 * @描述 首页
 */
public class HomeFragment extends LazyFragment<IHomeFgView, HomeFgPresenter> implements IHomeFgView {

    private final String TAG = "HomeFragment";

    @Override
    protected void init(View view) {
        setTitle(mPresenter.getSp("COMPANY"));
        DataWorkController.initWork();
        mPresenter.initAdapter();
        mPresenter.setData();
        mPresenter.getMessageCount();
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mPresenter.getMessageCount();
            }

        });
    }

    @Override
    protected HomeFgPresenter createPresenter() {
        return new HomeFgPresenter((MainActivity) getActivity(), this);
    }

    @Override
    public void onDestroy() {
        mPresenter.onDestroy();
        super.onDestroy();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_home;
    }

    @Override
    public RecyclerView getRvContent() {
        return mRvContent;
    }

    @Override
    public SmartRefreshLayout getRefreshLayout() {
        return mRefreshLayout;
    }

    /**
     * 留着
     * 刷新工作页面模块消息数
     */
    public void refreshPatrolMessage(){
        if(mPresenter != null){
            mPresenter.setData();
        }
    }

    /**
     * 刷新工作页面模块消息数
     * @param nowRefresh true 直接刷新 false提醒页面需要刷新
     */
    public void refreshHomeMessage(boolean nowRefresh){
        if(mPresenter == null){
            return;
        }
        if(nowRefresh){
            mPresenter.onResume();
        }else{
            mPresenter.setRefresh(true);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if(mPresenter != null){
                mPresenter.onResume();
            }
        } else {
            if(mPresenter != null){
                mPresenter.onPause();
            }
        }
    }

}

